package com.jsjvm.client.dbg;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.jsjvm.client.classparser.JClass;
import com.jsjvm.client.classparser.JMember.JMethod;
import com.jsjvm.server.JDWPPollImpl;
import com.jsjvm.shared.jdwp.data.JDWPLocation;
import com.jsjvm.shared.jdwp.events.JDWPClassPrepareEvent;
import com.jsjvm.shared.jdwp.events.JDWPEvent;
import com.jsjvm.shared.vmcmds.VMCommand;

/**
 * Performs the RPC (for testing calls the server function directly)
 * 
 * XXX Change to call via AJAX
 */
public class DbgUtil {
	private static JDWPPollImpl impl = new JDWPPollImpl();
	private static ObjectIDManager obj = ObjectIDManager.getInstance();

	public static void pollForCommands(JDWPEvent[] events,
			AsyncCallback<VMCommand> callback) {
		VMCommand cmd = impl.pollForCommands(events);
		if (cmd == null){
			callback.onFailure(new Exception("server dbg failure!"));
		}
		callback.onSuccess(cmd);
	}

	public static JDWPClassPrepareEvent classPreparedEventFromJClass(JClass jc){
		int classID = obj.assignObjectID(jc);
		String signature = "L" + jc.getName() + ";";
		return new JDWPClassPrepareEvent(classID, signature);
	}
	
	public static JDWPLocation createLocation(JClass c, JMethod m, int pc){
		int classID = obj.assignObjectID(c);
		int methodID = m.getID();
		return new JDWPLocation(classID, methodID, pc);
	}

}
